<template>
  <view class="container">
    <!-- 页面头部标题 -->
    <view class="new-page-header">
      <!-- 状态栏占位 -->
      <view class="new-status-bar" :style="{ height: statusBarHeight + 'px' }"></view>
      <!-- 导航栏 -->
      <view class="new-nav-bar" :style="{ height: navBarHeight + 'px' }">
        <!-- 返回按钮 -->
        <view class="new-back-button" @click="goBack">
          <u-icon name="arrow-left" size="36" color="#ffffff"></u-icon>
        </view>
        <!-- 标题 -->
        <view class="new-header-title" :style="{ lineHeight: navBarHeight + 'px' }">故障报修</view>
      </view>
    </view>

    <!-- 报修表单 -->
    <view class="repair-form">
      <!-- 产品编码输入 -->
      <view class="form-item">
        <input class="code-input" type="text" placeholder="请输入产品编码" v-model="productCode" />
        <view class="scan-icon" @click="scanCode">
          <u-icon name="scan" size="48" color="#74b9ff"></u-icon>
        </view>
      </view>

      <!-- 故障类型选择 -->
      <view class="form-item">
        <view class="option-item" :class="{ active: selectedType === 1 }" @click="selectType(1)">
          <text class="option-text">无法开锁</text>
          <u-icon name="checkmark-circle-fill" size="48" :color="selectedType === 1 ? '#74b9ff' : '#e9ecef'"></u-icon>
        </view>
        <view class="option-item" :class="{ active: selectedType === 2 }" @click="selectType(2)">
          <text class="option-text">无法关锁</text>
          <u-icon name="checkmark-circle-fill" size="48" :color="selectedType === 2 ? '#74b9ff' : '#e9ecef'"></u-icon>
        </view>
      </view>

      <!-- 故障描述 -->
      <view class="form-item">
        <textarea class="description-input" placeholder="请输入您遇到的故障描述" v-model="description" maxlength="200"></textarea>
      </view>

      <!-- 提交按钮 -->
      <view class="submit-section">
        <view class="submit-btn" :class="{ disabled: !canSubmit }" @click="handleSubmit">
          <text class="submit-text">提交</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import navbarMixin from "@/common/mixin/navbar.js";
import { reportRepair } from "@/api/index.js";

export default {
  mixins: [navbarMixin],

  data() {
    return {
      // 产品编码
      productCode: "",
      // 选中的故障类型
      selectedType: 1,
      // 故障描述
      description: "",
    };
  },

  computed: {
    /**
     * 是否可以提交
     */
    canSubmit() {
      return (
        this.productCode.trim() !== "" &&
        this.selectedType !== "" &&
        this.description.trim() !== ""
      );
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 如果从扫码页面跳转过来，获取扫码结果
    if (options.code) {
      this.productCode = decodeURIComponent(options.code);
    }
  },

  methods: {
    /**
     * 返回上一页
     */
    goBack() {
      uni.navigateBack({
        delta: 1,
      });
    },

    /**
     * 扫码获取产品编码
     */
    scanCode() {
      const app = this;
      uni.scanCode({
        scanType: ["qrCode", "barCode"],
        success(res) {
          console.log('res.result', res.result);

          app.productCode = encodeURIComponent(
            res?.result?.split("kpl/")?.[1])

        },
        fail(err) {
          if (err.errMsg.includes("cancel")) {
            return;
          }
          uni.showToast({
            title: "扫码失败，请重试",
            icon: "none",
          });
        },
      });
    },

    /**
     * 选择故障类型
     */
    selectType(type) {
      this.selectedType = type;
    },

    /**
     * 提交故障报修
     */
    handleSubmit() {
      if (!this.canSubmit) {
        uni.showToast({
          title: "请填写完整信息",
          icon: "none",
        });
        return;
      }

      const _that = this;

      uni.showModal({
        title: "确认提交",
        content: "确定要提交故障报修吗？",
        success: async (res) => {
          if (res.confirm) {
            uni.showLoading({
              title: "提交中...",
            });

            try {
              await reportRepair({
                kpl: _that.productCode,
                content: _that.description,
                fault_types_id: _that.selectedType,
              });

              uni.hideLoading();

              uni.showToast({
                title: "提交成功",
                icon: "success",
              });

              uni.navigateBack({
                delta: 1,
              });
            } catch (error) {
              console.log("errorerrorerror", error);

              uni.hideLoading();
              uni.showToast({
                title: "提交失败",
                icon: "error",
              });
            }

            // 模拟提交过程
            // setTimeout(() => {
            //   uni.hideLoading();
            //   uni.showToast({
            //     title: "提交成功",
            //     icon: "success",
            //   });

            //   // 提交成功后返回
            //   setTimeout(() => {
            //     uni.navigateBack({
            //       delta: 1,
            //     });
            //   }, 1500);
            // }, 2000);
          }
        },
      });
    },
  },

  /**
   * 分享当前页面
   */
  onShareAppMessage() {
    return {
      title: "故障报修",
      path: "/pages/newrepair/index",
    };
  },
};
</script>

<style lang="scss" scoped>
.container {
  min-height: 100vh;
  background: #f8f9fa;
}

.repair-form {
  padding: 40rpx 30rpx;

  .form-item {
    background: #ffffff;
    border-radius: 20rpx;
    padding: 30rpx;
    margin-bottom: 30rpx;
    box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);

    &:first-child {
      display: flex;
      align-items: center;

      .code-input {
        flex: 1;
        font-size: 28rpx;
        color: #333333;
        border: none;
        outline: none;

        &::placeholder {
          color: #cccccc;
        }
      }

      .scan-icon {
        width: 60rpx;
        height: 60rpx;
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }

    .option-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 20rpx 0;

      &:not(:last-child) {
        border-bottom: 1rpx solid #f5f5f5;
        margin-bottom: 20rpx;
      }

      .option-text {
        font-size: 28rpx;
        color: #333333;
      }

      &.active {
        .option-text {
          color: #74b9ff;
        }
      }
    }

    .description-input {
      width: 100%;
      min-height: 200rpx;
      font-size: 28rpx;
      color: #333333;
      line-height: 1.6;
      border: none;
      outline: none;
      resize: none;

      &::placeholder {
        color: #cccccc;
      }
    }
  }

  .submit-section {
    margin-top: 80rpx;

    .submit-btn {
      width: 100%;
      height: 100rpx;
      border-radius: 50rpx;
      display: flex;
      align-items: center;
      justify-content: center;
      background: #cccccc;

      .submit-text {
        font-size: 32rpx;
        font-weight: 600;
        color: #ffffff;
      }

      &:not(.disabled) {
        background: #74b9ff;
        box-shadow: 0 8rpx 24rpx rgba(116, 185, 255, 0.4);

        &:active {
          transform: translateY(2rpx);
          box-shadow: 0 6rpx 20rpx rgba(116, 185, 255, 0.5);
        }
      }
    }
  }
}
</style>
